<template>
  <div>
    <!--  查询  -->
    <el-card class="dis">
      <el-form :inline="true" :model="formInline" class="demo-form-inline cardH">
        <el-form-item label="模板名称">
          <el-input v-model="formInline.name" size="mini" clearable placeholder="请输入等级名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="search">查询</el-button>
          <el-button type="primary" size="mini" @click="addOrEdit(0)">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 列表 -->
    <el-card style="margin-top: 20px">
      <!--  table  -->
      <el-table
        :data="tableData"
        style="width: 100%;margin-top: 20px"
        size="mini"
      >
        <el-table-column
          prop="id"
          label="模板id"
          align="center"
          width="100"
        />
        <el-table-column
          prop="name"
          label="模板名称"
          align="center"
        />
        <el-table-column
          label="报警类型"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag type="success" effect="plain">{{ scope.row.type === 1 ? '数据匹配' : '设备上报' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="info"
              size="mini"
              @click="info(scope.row)"
            >
              详情
            </el-button>
            <el-button
              type="warning"
              size="mini"
              @click="addOrEdit(scope.row.id)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              size="mini"
              @click="del(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--   分页   -->
<!--      <el-pagination-->
<!--        style="float: right;"-->
<!--        :page-size="10"-->
<!--        :current-page="formInline.offset"-->
<!--        layout="total, prev, pager, next, jumper"-->
<!--        :total="total"-->
<!--        @current-change="handleCurrentChange"-->
<!--      />-->
      <pagination style='float: right;' v-show='total>0' :total='total' :page.sync='formInline.offset' :limit.sync='formInline.limit' @pagination='getList' />
    </el-card>
    <!--详情组件-->
    <Detail
      ref="warningTemDetail"
      :id="id"
      @reload="reload"
      @resetId = 'resetId'
    />
    <Info
      ref="warningInfo"
      @resetId = 'resetId'
    />
  </div>
</template>

<script>
  import { warningTemList, deleteWarningTem } from '@/api/warning'
  import Detail from './components/detail'
  import Info from './components/info'
  export default {
    name: "index",
    components: {
      Detail,
      Info
    },
    data() {
      return{
        id: 0, // 编辑id
        total: 0, // 数据总量
        tableData: [], // 列表数据
        // 列表搜索数据
        formInline: {
          sort: 'created_at',
          offset: 1,
          limit: 10,
          order: 'descending',
          name: ''
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /**
       * @description: 删除数据
       * @author: hst
       */
      del(id) {
        this.$confirm('是否要删除此条数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(res => {
          deleteWarningTem({id}).then(res1 => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getList()
          })
        })
      },
      /**
       * @description: 重置列表
       * @author: hst
       */
      reload() {
        this.formInline.offset = 1
        this.formInline.name = ''
        this.getList()
      },
      /**
       * @description: 获取列表数据
       * @author: hst
       */
      getList() {
        warningTemList(this.formInline).then(res => {
          this.tableData = res.data.list
          this.total = res.data.total
        })
      },
      // 指定页码数据
      handleCurrentChange(val) {
        this.formInline.offset = val
        this.getList()
      },
      /**
       * @description: 查询
       * @author: 书童
       */
      search() {
        this.formInline.offset = 0
        this.getList()
      },
      /**
       * @description: 新增
       * @author: 书童
       */
      addOrEdit(id) {
        this.id = id
        this.$refs.warningTemDetail.open()
      },
      /**
       * @description: 查看详情
       * @author: 书童
       */
      info(row) {
        this.$refs.warningInfo.open(row)
      },
      resetId() {
        this.id = 0
      }
    }
  }
</script>

<style lang="scss" scoped>
  .cardH{
    height: 38px;
  }
</style>

